//类型type 0表示天地图 1表示网络墨卡托
class TileHelper {
    static  tdt_x_orgin= -180;
    static  tdt_y_orgin= 90;
    static  tdt_resolutions=[
        0.703125,//1 0.703125
        0.3515625,//2
        0.17578125,//3
        0.087890625,//4
        0.0439453125,//5
        0.02197265625,//6
        0.010986328125,//7
        0.0054931640625,//8
        0.00274658203125,//9
        0.001373291015625,//10
        0.0006866455078125,//11
        0.00034332275390625,//12
        0.000171661376953125,//13
        0.0000858306884765625,//14
        0.00004291534423828125,//15
        0.00002145767211914062,//16
        0.00001072883605957031,//17
        0.000005364418029785155,//18
        0.0000026822090148925775,//19
        0.00000134110450744628875,//20
        0.000000670552253723144375//21 0.0000003352761269
    ]
    static  tdt_scales=[
        295829355.45,//1
        147914677.73,//2
        73957338.86,//3
        36978669.43,//4
        18489334.72,//5
        9244667.36,//6
        4622333.68,//7
        2311166.84,//8
        1155583.42,//9
        577791.71,//10
        288895.85,//11
        144447.93,//12
        72223.96,//13
        36111.98,//14
        18055.99,//15
        9028.00,//16
        4514.00,//17
        2257.00,//18
        1128.50,//19
        564.25,//20
        282.13//21
    ]
    static GetResolution(zoom,type=0)
    {
        zoom=zoom-0
        type=type-0
        let res = 0.0;
        if (type==0)
        {
            if (zoom<=21)
            {
                res = TileHelper.tdt_resolutions[zoom-1];
            }
        }
        else if (type == 1)
        {
            let a = Math.pow(2, (18 - zoom));
            res = 0.5971642833709717 * a;  //第18级的分辨率
        }
        return res;
    }
    static Get_Row_Col_by_Min_Max(Xmin,Xmax,Ymin,Ymax,zoom,type = 0,TileSize = 256)
    {
        Xmin=Xmin-0
        Xmax=Xmax-0
        Ymin=Ymin-0
        Ymax=Ymax-0
        zoom=zoom-0
        type=type-0
        TileSize=TileSize-0
        let ColMin=0;
        let ColMax=0;
        let RowMin=0;
        let RowMax=0;
        if (type == 0)
        {
            if (zoom <= 21)
            {
                let d_t = TileHelper.GetResolution(zoom, type) * TileSize; //一个切片的宽度 或者长度 //
                RowMin =parseInt(Math.floor((Xmin - TileHelper.tdt_x_orgin) / d_t))
                RowMax = parseInt(Math.floor((Xmax - TileHelper.tdt_x_orgin) / d_t));
                ColMin = parseInt(Math.floor((TileHelper.tdt_y_orgin - Ymax) / d_t));
                ColMax = parseInt(Math.floor((TileHelper.tdt_y_orgin - Ymin) / d_t));
            }
        }
        else if (type == 1)
        {
            let _x = -156543.0339 * 128; //就是起始的坐标（-20037508, -20037508,20037508, 20037508.34）//
            let res = TileHelper.GetResolution(zoom,1);
            let spanXY = res * TileSize;//zu[0]为1px代表的纬度,this.imgSize为加载的图片大小(256)  // XY是一样的
            let xminl = Xmin - _x;
            let xmaxl = Xmax - _x;
            let ymaxl = -Ymin - _x;
            let yminl = -Ymax - _x;
            let bx_min = Math.abs(xminl / spanXY);
            let bx_max = Math.abs(xmaxl / spanXY);
            let by_min = Math.abs(yminl / spanXY);
            let by_max = Math.abs(ymaxl / spanXY);
            RowMin = parseInt(Math.floor(bx_min));
            RowMax = parseInt(Math.floor(bx_max));
            ColMin = parseInt(Math.floor(by_min));
            ColMax = parseInt(Math.floor(by_max));
        }
        return [ ColMin, ColMax, RowMin, RowMax ] ;
    }
    //根据切片的行列号 级别 获取 该切片的_Xmin Xmax Ymin Ymax
    static Get_Tile_Extent(Row,Column,Zoom,type = 0,TileSize = 256)
    {
        Column=Column-0
        Row=Row-0
        Zoom=Zoom-0
        type=type-0
        TileSize=TileSize-0
        let d_Ymin_tile=0;
        let d_Ymax_tile=0;
        let d_Xmax_tile=0;
        let d_Xmin_tile=0;
        if (type == 0)
        {
            if (Zoom <= 21)
            {
                let d_t = TileHelper.GetResolution(Zoom, type) * TileSize; //一个切片的宽度 或者长度 //
                d_Ymin_tile = TileHelper.tdt_y_orgin - (Row + 1) * d_t;
                d_Ymax_tile = TileHelper.tdt_y_orgin - Row * d_t;
                d_Xmax_tile = TileHelper.tdt_x_orgin + (Column + 1) * d_t;
                d_Xmin_tile = TileHelper.tdt_x_orgin + Column * d_t;
            }
        }
        else if (type == 1)
        {
            let _x = -156543.0339 * 128; //就是起始的坐标（-20037508, -20037508,20037508, 20037508.34）
            let res = TileHelper.GetResolution(Zoom, type);
            let spanXY = res * TileSize;
            d_Xmin_tile = Column * spanXY + _x;
            d_Xmax_tile = Column * spanXY + _x + spanXY;
            d_Ymin_tile = -_x - Row * spanXY - spanXY;
            d_Ymax_tile = -_x - Row * spanXY;
        }
        return [ d_Xmin_tile, d_Xmax_tile, d_Ymin_tile, d_Ymax_tile ];
    }
    // 根据XY坐标、级别获取切片的列 行
    static Get_Row_Col_FromPoint(X, Y, zoom, type = 0, TileSize = 256)
    {
        X=X-0
        Y=Y-0
        zoom=zoom-0
        type=type-0
        TileSize=TileSize-0
        let RowMin=0;
        let ColMin=0;
        if (type == 0)
        {
            if (zoom <= 21)
            {
                let d_t = TileHelper.GetResolution(zoom, type) * TileSize; //一个切片的宽度 或者长度 //
                ColMin=parseInt(Math.floor((X - TileHelper.tdt_x_orgin) / d_t));
                RowMin = parseInt(Math.floor((TileHelper.tdt_y_orgin - Y) / d_t));
            }
        }
        else if (type == 1)
        {
            let _x = -156543.0339 * 128; //就是起始的坐标（-20037508, -20037508,20037508, 20037508.34）
            let res = TileHelper.GetResolution(zoom, 1);
            let d_t = res * TileSize; //一个切片的宽度 或者长度 //
            ColMin = parseInt(Math.floor((X - _x) / d_t));
            RowMin = parseInt(Math.floor((-(_x) - Y) / d_t));
        }
        return  [ RowMin, ColMin ];
    }
}
export function TileHelper_set() {
    global["vapp"].config.globalProperties.$tileHelper = TileHelper
}